<blockquote>
    <p>阿里Oss文件上传</p>
    <footer>
        <p>需要在阿里购买Oss云存储服务, 以下配置替换成自己购买的</p>
        <p>默认缺省配置：</p>
        <p>#OSS存储</p>
        <p>ants.alioss.url=http://ants-m.oss-cn-beijing.aliyuncs.com</p>
        <p>ants.alioss.access-key-id=LTAIGtncqD2CaDvW</p>
        <p>ants.alioss.access-key-secret=z6RtuvSVFqcCnn3rsYT6z4L9RcwWZH</p>
    </footer>
</blockquote>
<blockquote>
    <p>示例 需要添加Oss Maven依赖</p>
</blockquote>
<pre class="code brush:xml">
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>2.2.1</version>
 </dependency>
</pre>
<blockquote>
    <p>示例 上传文件到OSS</p>
</blockquote>
<pre class="code brush:java">
package cn.jants.demos;


import cn.jants.common.annotation.action.Controller;
import cn.jants.common.annotation.action.POST;
import cn.jants.common.annotation.boot.PropertyConfiguration;
import cn.jants.common.annotation.service.Application;
import cn.jants.common.utils.ImageUtil;
import cn.jants.core.startup.JTomcat;
import cn.jants.plugin.oss.AliOssTpl;
import cn.jants.plugin.oss.OssResult;
import cn.jants.plugin.tool.AliOssTool;
import cn.jants.restful.render.Json;

import javax.servlet.http.Part;
import java.io.IOException;


/**
 * 上传文件示例
 *
 * @author MrShun
 * @version 1.0
 * @Date 2017/12/20
 */
@Application
@PropertyConfiguration
@Controller
public class DemosApplication {

    /**
     * 上传用户头像
     *
     * @param txImg file input 名称
     * @return
     */
    @POST("/uploadAvatar")
    public Object uploadAvatar(Part txImg) throws IOException {
        boolean check = ImageUtil.check(txImg.getContentType());
        if (check) {
            long size = txImg.getSize();
            if (size > 200000) {
                return Json.fail("错误, 上传图片大小不能超过[200kb]!");
            }
            String submittedFileName = txImg.getSubmittedFileName();
            String fileName = "tx" + System.currentTimeMillis() + submittedFileName.substring(submittedFileName.lastIndexOf("."), submittedFileName.length());
            AliOssTpl ossTpl = AliOssTool.getAliOss();
            OssResult result = ossTpl.uploadStream2OSS(txImg.getInputStream(), fileName, "auth/upload/avatar/");
            return Json.success(result.getUrl());
        } else {
            return Json.fail("请选择jpg 或者 png 图片文件进行上传!");
        }
    }

    public static void main(String[] args) {
        JTomcat.run(DemosApplication.class, 80, "/demo");
    }
}

</pre>
<script type="text/javascript">
    $(function () {
        SyntaxHighlighter.highlight();
        $(".toolbar").remove();
    });
</script>